package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialBreakageApprovalEntity;
import city.spring.modules.material.entity.MaterialBreakageDeliveryEntity;
import city.spring.modules.material.entity.MaterialBreakageWarehouseEntity;
import city.spring.modules.material.repository.MaterialBreakageApprovalRepository;
import city.spring.modules.material.service.MaterialBreakageApprovalService;
import city.spring.modules.material.service.MaterialBreakageDeliveryService;
import city.spring.modules.material.service.MaterialBreakageWarehouseService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：物资报损审核表
 *
 * @author 谢金婷
 * @date 2021-01-20 14:22:53
 */
@CacheConfig(cacheNames = {MaterialBreakageApprovalServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MaterialBreakageApprovalServiceImpl extends ServiceImpl<MaterialBreakageApprovalRepository, MaterialBreakageApprovalEntity> implements MaterialBreakageApprovalService {

    private final UserService userService;
    private final MaterialBreakageWarehouseService materialBreakageWarehouseService;
    private final MaterialBreakageDeliveryService materialBreakageDeliveryService;

    public MaterialBreakageApprovalServiceImpl(UserService userService, MaterialBreakageWarehouseService materialBreakageWarehouseService, MaterialBreakageDeliveryService materialBreakageDeliveryService) {
        this.userService = userService;
        this.materialBreakageWarehouseService = materialBreakageWarehouseService;
        this.materialBreakageDeliveryService = materialBreakageDeliveryService;
    }

    @Override
    public void saveMaterialBreakageApproval(MaterialBreakageApprovalEntity entity) {
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setReviewer(userEntity.getId());
        entity.setType("报损入库");

        MaterialBreakageWarehouseEntity materialBreakageWarehouseEntity = materialBreakageWarehouseService.getById(entity.getMaterialBreakageId());
        materialBreakageWarehouseEntity.setStatus(entity.getApprovalStatus());
        materialBreakageWarehouseService.lambdaUpdate()
                .set(MaterialBreakageWarehouseEntity::getStatus,materialBreakageWarehouseEntity.getStatus())
                .eq(MaterialBreakageWarehouseEntity::getId,materialBreakageWarehouseEntity.getId())
                .update();
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }
    @Override
    public void addMaterialBreakageApproval(MaterialBreakageApprovalEntity entity) {
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setReviewer(userEntity.getId());
        entity.setType("报损出库");

        MaterialBreakageDeliveryEntity materialBreakageDeliveryEntity = materialBreakageDeliveryService.getById(entity.getMaterialBreakageId());
        materialBreakageDeliveryEntity.setStatus(entity.getApprovalStatus());
        materialBreakageDeliveryService.lambdaUpdate()
                .set(MaterialBreakageDeliveryEntity::getStatus,materialBreakageDeliveryEntity.getStatus())
                .eq(MaterialBreakageDeliveryEntity::getId,materialBreakageDeliveryEntity.getId())
                .update();
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMaterialBreakageApproval(MaterialBreakageApprovalEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMaterialBreakageApproval(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMaterialBreakageApproval(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}